const app = getApp();
const util = require('../../utils/util.js')
import Https from "../../utils/api.js"
const http = new Https
Page({
  data: {
    scrollWithAnimation: false,
    myId: 1,
    sendId: 1,
    recId: 2,
    InputBottom: 0,
    sendMsg: '',
    scrollTop: 0,
    high: 0,
    list: [],
    image: wx.getStorageSync('userInfo').avatar
  },
  onLoad(e) {
    console.log(e)
    http.request('decode',{
      secret:wx.getStorageSync('userInfo').id
    }).then(res=>{
      console.log(res)
      this.setData({
        myId:res.data.id,
        sendId:res.data.id,
      })
    })
    this.setData({
      recId: Number(e.id)
    })
    wx.setNavigationBarTitle({
      title: e.name,
    })
    setTimeout(()=>{
      this.sync()
    },2000)

    this.setData({
      scrollWithAnimation: true
    })
  },

   sync(){
    let send_content = wx.getStorageSync(`${this.data.sendId}to${this.data.recId}`) || []
     console.log(this.data.sendId)
    let rec_content = wx.getStorageSync(`${this.data.recId}to${this.data.sendId}`) || []
    let content = rec_content.concat(send_content)
    console.log(content)
    this.setData({
      list: content,
      scrollTop: 999999999999999,
    })
   },
 

  onShow: function () {
    var that = this;
    wx.setStorageSync(`chat${that.data.sendId}to${that.data.recId}`, 0)
 
    wx.onSocketMessage(function (data) {
      console.log("onSocketMessage ", data)
      let message = JSON.parse(data.data)
      // 接收这个人发送给我的消息
      if (that.data.recId == message.send_id) {
        let array = wx.getStorageSync(`${that.data.recId}to${message.rec_id}`) || []
        array.push(message)
        wx.setStorageSync(`${that.data.recId}to${message.rec_id}`, array)
        wx.setStorageSync(`chat${that.data.myId}to${that.data.send_id}`, 0)
        that.sync()
        that.setData({
          scrollTop: 999999999999999
        })
      }
      // 接收自己发送的消息
      else if (message.send_id == that.data.sendId && message.rec_id == that.data.recId) {
        let array = wx.getStorageSync(`${that.data.myId}to${message.rec_id}`) || []
        array.push(message)
        wx.setStorageSync(`${that.data.myId}to${message.rec_id}`, array)
        wx.setStorageSync(`chat${that.data.myId}to${that.data.recId}`, 0)
        that.sync()
        that.setData({
          scrollTop: 999999999999999
        })
      }

      // 接收其他人发送给我的消息
      else {
        let array = wx.getStorageSync(`${that.data.recId}to${message.rec_id}`) || []
        array.push(message)
        wx.setStorageSync(`${that.data.recId}to${message.rec_id}`, array)
        wx.setStorageSync(`chat${that.data.myId}to${that.data.send_id}`, 0)
        that.sync()
        that.setData({
          scrollTop: 999999999999999
        })
        // let array = wx.getStorageSync(`${that.data.myId}to${message.sendId}`) || []
        // array.push(message)
        // wx.setStorageSync(`${that.data.myId}to${message.sendId}`, array)
        // let count = wx.getStorageSync(`chat${that.data.myId}to${message.sendId}`) || 0
        // wx.setStorageSync(`chat${that.data.myId}to${message.sendId}`, count + 1)
      }
    })
  },
  send() {
    let that = this
    wx.sendSocketMessage({
      data: JSON.stringify(
        {
          create_time: util.formatTime(new Date()),
          send_id: that.data.sendId,
          image: that.data.image,
          name:wx.getStorageSync('userInfo').nick_name,
          rec_id: this.data.recId,
          content: this.data.sendMsg,
          identity:"agent",
          flag:"message"
        }),
      success: function (res) {
        let array = wx.getStorageSync(`${that.data.recId}to${that.data.sendId}`) || []
        array.push({
          create_time: util.formatTime(new Date()),
          send_id: that.data.sendId,
          image: that.data.image,
          name:"卢中原",
          rec_id: that.data.recId,
          content: that.data.sendMsg,
          flag:"message"
        })
        wx.setStorageSync(`${that.data.recId}to${that.data.sendId}`, array)
        that.sync()
        that.setData({
          sendMsg: ''
        })
        console.log("sendSocketMessage 成功")
      },
      fail: function (res) {
        console.log("sendSocketMessage 失败")
        wx.showToast({
          title: '发送消息失败',
          icon: 'none'
        })
      }
    });
  },

  InputFocus(e) {
    this.setData({
      InputBottom: e.detail.height,
      high: e.detail.height,
    })
    this.setData({
      scrollTop: 99999999999
    })
  },
  //发送消息
  bindinput(e) {
    console.log(e.detail.value)
    this.setData({
      sendMsg: e.detail.value
    })
  },
  InputBlur(e) {
    this.setData({
      InputBottom: 0,
      high: 0
    })
  },
})